module.exports = {
	msg(title, position='center', duration=2500, mask=false, icon='none'){
		//统一提示方便全局修改
		if(Boolean(title) === false){
			return;
		}
		uni.showToast({
			title:title,
			duration:duration,
			mask:mask,
			icon:icon
		});
	},
	//本地存储
	storage(key){
		const val = uni.getStorageSync(key);
		return val;
	},
	
	// 获取上一页实例
	lastPage(){
		let pages = getCurrentPages();
		let lastPage = pages[pages.length - 2];
		// #ifdef H5
		return lastPage;
		// #endif
		return lastPage.$vm;
	},
	//登陆拦截，第一个参数为回调页面(默认跳转指定页面),第二个参数为是否tabbar页面，1是，0为默认不是，2为不是并且请求用户信息接口(并且跳转指定页面),3为不是并且请求用户接口(并且返回当前页面),4为不是并且重定向到登录页面
	showLogin(backUrl,isTabbar){
		uni.setStorageSync('backUrl', backUrl);
		uni.setStorageSync('isTabbar', isTabbar?isTabbar:0);
		if(isTabbar==4){
			uni.redirectTo({
				url:'/pages/login/login',
			})
		}else{
			uni.navigateTo({
				url:'/pages/login/login',
			})
		}
	},
	insert(param) { //队列插入数据
		// if(param.value.length>10){
		// 	return;
		// }
		param.maxNum = param.maxNum || 10; //队列容量 默认队列中超过10条数据，自动删除尾部
		let data = uni.getStorageSync(param.key)||[];
		let findIndex = -1;
		data.map((item,index)=>{
			if (item.userName == param.value.userName){
				findIndex = index;
			}
		});
		//如果历史记录中不存在则在数组开头添加，否则找到该项索引删除并且在开头添加
		if (findIndex == -1) {
			data.unshift(param.value);
		} else {
			data.splice(findIndex, 1);
			data.unshift(param.value);
		}
		//最多param.maxNum个纪录
		data.length > param.maxNum && data.pop();
		uni.setStorageSync(param.key, data);
	},
	// 序列化时间
	timeFormat(time, params) {
		var d = time ? new Date(time) : new Date(),
			year = d.getFullYear(),
			month = d.getMonth() + 1,
			day = d.getDate(),
			hours = d.getHours(),
			minutes = d.getMinutes(),
			seconds = d.getSeconds();
	
		if (month < 10) month = '0' + month;
		if (day < 10) day = '0' + day;
		if (hours < 10) hours = '0' + hours;
		if (minutes < 10) minutes = '0' + minutes;
		if (seconds < 10) seconds = '0' + seconds;
	
		if (params) {
			return {
				year: year,
				month: month,
				day: day
			};
		} else {
			return (year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds);
		}
	},
	// 获取url参数的方法
	GetQueryString(url,name){
	     let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	     let index=url.lastIndexOf("?");
	     let r=url.substring(index+1,url.length).match(reg);
	     if(r!=null)return  unescape(r[2]); return null;
	},
	// 校验手机号
	checkPhone(phone){
		let reg=/^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/;
		if(reg.test(phone))return true; return false;
	},
    formatTime(time) {
		if (typeof time !== 'number' || time < 0) {
			return time
		}
	
		var hour = parseInt(time / 3600)
		time = time % 3600
		var minute = parseInt(time / 60)
		time = time % 60
		var second = time
	
		return ([hour, minute, second]).map(function (n) {
			n = n.toString()
			return n[1] ? n : '0' + n
		}).join(':')
	},
	
	formatLocation(longitude, latitude) {
		if (typeof longitude === 'string' && typeof latitude === 'string') {
			longitude = parseFloat(longitude)
			latitude = parseFloat(latitude)
		}
	
		longitude = longitude.toFixed(2)
		latitude = latitude.toFixed(2)
	
		return {
			longitude: longitude.toString().split('.'),
			latitude: latitude.toString().split('.')
		}
	},
	dateUtils: {
		UNITS: {
			'年': 31557600000,
			'月': 2629800000,
			'天': 86400000,
			'小时': 3600000,
			'分钟': 60000,
			'秒': 1000
		},
		humanize: function (milliseconds) {
			var humanize = '';
			for (var key in this.UNITS) {
				if (milliseconds >= this.UNITS[key]) {
					humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
					break;
				}
			}
			return humanize || '刚刚';
		},
		format: function (dateStr) {
			var date = this.parse(dateStr)
			var diff = Date.now() - date.getTime();
			if (diff < this.UNITS['天']) {
				return this.humanize(diff);
			}
			var _format = function (number) {
				return (number < 10 ? ('0' + number) : number);
			};
			return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' +
				_format(date.getHours()) + ':' + _format(date.getMinutes());
		},
		parse: function (str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
			var a = str.split(/[^0-9]/);
			return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
		}
	},
	// 校验手机号
	checkPhone(phone) {
		let reg = /^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/;
		if (phone.length != 11 || !reg.test(phone)) {
		        return false; // 不是中国大陆手机号格式或者不是11位数字
		    } else {
		        return true; // 中国大陆手机号格式且为11位数字
		}
	},
	// 当前日期、时间,params:date,time,dateTime,month
	getNow(params) {
		var d =  new Date(),
			year = d.getFullYear(),
			month = d.getMonth() + 1,
			day = d.getDate(),
			hours = d.getHours(),
			minutes = d.getMinutes(),
			seconds = d.getSeconds();
	
		if (month < 10) month = '0' + month;
		if (day < 10) day = '0' + day;
		if (hours < 10) hours = '0' + hours;
		if (minutes < 10) minutes = '0' + minutes;
		if (seconds < 10) seconds = '0' + seconds;
		
		if(params=='time'){
			return (hours + ':' + minutes + ':' + seconds);
		}else if(params=='dateTime'){
			return (year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds);
		}else if(params=='month'){
			return (year + '-' + month);
		}else{
			return (year + '-' + month + '-' + day);
		}
	},
	trim(str) {
		return str.replace(/\s/g, "");
	},
	// 设备数据解码
	deviceDecode(pValue, len) {
		let tmp = 0;
		let hibit = 0;
		let lobit = 0;
		let hibit1 = 0;
		let lobit1 = 0;
		for (let i = len - 1; i > 0; i--) {
		  tmp = pValue[i];
		  hibit1 = (tmp & 0x55) << 1;
		  lobit1 = (tmp & 0xAA) >> 1;
		  tmp = pValue[i - 1];
		  hibit = (tmp & 0x55) << 1;
		  lobit = (tmp & 0xAA) >> 1;
		  pValue[i] = ~(hibit1 | lobit);
		  pValue[i - 1] = ~(hibit | lobit1);
			pValue[i] = pValue[i] &0xff
			pValue[i-1] = pValue[i-1] &0xff
		}
		return pValue
	}
}